﻿using _11Net8AspNetCore_WebApis.Result;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace _11Net8AspNetCore_WebApis.Filter
{
    public class ResultActionFilter : IActionFilter
    {

        private ILogger<ResultActionFilter> _Logger = null;
        public ResultActionFilter(ILogger<ResultActionFilter> logger)
        {
            _Logger = logger;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            // 1、执行之后 获取结果
            IActionResult actionResult = context.Result;

            // 2、类型转换
            if (actionResult is ObjectResult objectResult)
            {
                // 3、获取结果值
                object result = objectResult.Value;

                // 4\包装结果
                CommonResult common = new CommonResult();
                common.Code = 200;
                common.message = "success";
                common.data = result;

                // 5\ 输出包装后的结果
                context.Result = new ObjectResult(common);
            }
        }

        /// <summary>
        /// Action 执行之前调佣
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnActionExecuting(ActionExecutingContext context)
        {

        
        }
    }
}
